/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.mmaingenieria.GestorFacturacion.model;

import com.mmaingenieria.GestorFacturacion.model.interfaces.InterfaceUsuarioDAO;

import java.sql.Connection;
import java.sql.SQLException;
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;
import javax.sql.DataSource;

/**
 *
 * @author Mikle
 */
public class MySqlDAOFactory extends DAOFactory{
    //inicializo un DataSource (para utilizar el context.xml
    private static DataSource ds = null;
    
    //Constructor
    //************************************************************************
    public MySqlDAOFactory(){
        try {
            Context ictx = new InitialContext();
            Context ctx = (Context) ictx.lookup("java:comp/env");
            ds = (DataSource) ctx.lookup("jdbc/GestorFacturacion");
        } catch (NamingException ex) {
        }
    }
    
    //Metodos
    //************************************************************************
    
    //Conectar con la base de datos
    public static Connection conectar() throws Exception{
        Connection conexion = null; //Creamos un tipo connection
        try {
            conexion = ds.getConnection();//Hago la conexsion con los datos del context
        } catch (Exception ex) {
        	ex.printStackTrace();
        }
        if ( ds==null){
        	throw new Exception("Problema al conectar BBDD ds=" + ds);
        }
        return conexion;
    }
    
    
    //Desconectar con la base de datos
    public static void desconectar(Connection conexion) {
        if(conexion!=null){
            try {
                conexion.close();
            } catch (SQLException ex) {
            }
        }
    }
    
    
    //Metodos que tengo que implementar al extender de DAOFActory
    //*************************************************************************
    @Override
    public InterfaceUsuarioDAO getInterfaceUsuarioDAO() {
        return new MySqlUsuarioDAO();
    }
     
    
}
